package com.alibaba.user.controller;

import com.alibaba.basic.exception.BusinessException;
import com.alibaba.basic.util.AjaxResult;
import com.alibaba.user.dto.BinderDto;
import com.alibaba.user.dto.LoginDto;
import com.alibaba.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RequestMapping("/login")
@RestController
class loginController {

    @Autowired
    private ILogininfoService logininfoService;

    @PostMapping("/vector")
    public AjaxResult vectorLogin(@RequestBody LoginDto loginDto) {

        try {
            Map<String, Object> map = logininfoService.vectorLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙,请稍后再试");
        }
    }

    @GetMapping("/wechat/{code}")
    public AjaxResult weachatLogin(@PathVariable("code") String code) {

        try {
            return logininfoService.wechatLogin(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙,请稍后再试");
        }
    }

    /**
     * 微信绑定接口
     *
     * @param binderDto
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody BinderDto binderDto) {
        try {
            return logininfoService.wechatBinder(binderDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统异常，请稍后重试!!!");
        }
    }

    @GetMapping("/logout/{token}")
    public AjaxResult logout(@PathVariable("token") String token) {
        try {
            logininfoService.logout(token);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统繁忙,请稍后再试");
        }
    }
}
